package com.zx.dsptuneup_cloud.widget.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import com.google.android.material.tabs.TabLayout;
import android.widget.TextView;
import com.zx.dsptuneup_cloud.R;
import java.util.ArrayList;
import java.util.List;

public class MyFrequentTabLayout extends TabLayout {
    private int selectedBtnBgResId = R.drawable.freq_rect_border_sel;
    private int normalBtnBgResId = R.drawable.freq_rect_border_normal;
//    private float textSize = 17f;
    private int textColor = R.color.white;
    private int selectedTextColor = R.color.freq_tag;
    private List<String> titles;


    public MyFrequentTabLayout(Context context) {
        super(context);
        init();
    }

    public MyFrequentTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyFrequentTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        titles = new ArrayList<>();
        this.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                /**
                 * 设置当前选中的Tab为特殊高亮样式。
                 */
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                    tab_text.setBackgroundResource(selectedBtnBgResId);
                    tab_text.setTextColor(getContext().getResources().getColor(selectedTextColor));
                    tab_text.setGravity(Gravity.CENTER);
//                    tab_text.setTextSize(textSize);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                /**
                 * 重置所有未选中的Tab颜色、字体、背景恢复常态(未选中状态)。
                 */
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                    tab_text.setBackgroundResource(normalBtnBgResId);
//                    tab_text.setTextSize(textSize);
                    tab_text.setTextColor(getContext().getResources().getColor(textColor));
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public void setTitle(List<String> titles) {
        this.titles = titles;
        /**
         * 开始添加切换的Tab。
         */
        for (String title : this.titles) {
            TabLayout.Tab tab = newTab();
            tab.setCustomView(R.layout.tablayout_frequent_item);

            int postion = this.titles.indexOf(title);
            if (tab.getCustomView() != null) {
                TextView tab_tag = tab.getCustomView().findViewById(R.id.tab_tag);
                tab_tag.setText(postion+1+"");

                TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                tab_text.setBackgroundResource(normalBtnBgResId);
                tab_text.setTextColor(getContext().getResources().getColor(textColor));
                tab_text.setText(title);

            }

            this.addTab(tab);
        }
    }


    /**
     *   数组长度没有发生变化，采用这种！！变化了采用MyWhiteTabLayout的方法
     * */
    public void refreshTitles(List<String> titles) {
        this.titles = titles;
        for (String title : this.titles) {
            int postion = this.titles.indexOf(title);
            TabLayout.Tab tab = this.getTabAt(postion);

            if (tab !=null && tab.getCustomView() != null) {
                TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                tab_text.setText(title);
            }

        }

    }
}
